#!/usr/bin/env python
# encoding: utf-8
"""
@Author: Beam
@Mail:506556658@qq.com
@file: DB_option.py
@time: 2017/6/10 10:16
"""

from DB_connetion_pool import getPTConnection, PTConnectionPool

# def MySQLOPT(sql):
#     #申请资源
#
#         try:
#             # 获取所有记录列表
#             db.cursor.execute(sql)
#             results = db.cursor.fetchall();
#             for row in results:
#                print(row)
#         except:
#             print ("Error: unable to fecth data")
#
#
#
# sql = "select * from student"
# MySQLOPT(sql)



class OperationMySQL(object):
    def selectSQL(self,sql):
        with getPTConnection() as db:
            db.cursor.execute(sql)
            results = db.cursor.fetchall();
        return results

    def insertSQL(self,sql):
        with getPTConnection() as db:
            db.cursor.execute(sql)
            db.cursor.commit()

    def updateSQL(self,sql):
        with getPTConnection() as db:
            db.cursor.execute(sql)
            db.cursor.commit()

    def deleteSQL(self,sql):
        with getPTConnection() as db:
            db.cursor.execute(sql)
            db.cursor.commit()



##测试sql

optmysql = OperationMySQL()
#select
sql = 'select * from student;'
result = optmysql.selectSQL(sql)
for i in result:
    print(i)
#insert
#sql = "insert into student (name,gender) values ('%s','%s');"  %('Beam','男')
#print(sql)
#optmysql.insertSQL(sql)



